<?php
/**
 * Class Web_controller
 * 前后台通用视图公用控制器
 */
class Web_Controller extends BS_Controller
{
    public $base_url = '';
    public function __construct()
    {
        parent::__construct();
        // 网址导入
        $this->load->config('config');
        $this->base_url = $this->config->item('base_url');
        $this->load->library('mobile_detect');
        $this->is_mobile = $this->mobile_detect->isMobile();
        //获取模块名(本项目分模块)
        $this->model_name = current($this->uri->segment_array());
    }

    /**
     * 通用视图加载器
     * @param  [type] $viewName [视图文件名]
     * @param  array  $data     [description]
     * @return [type]           [description]
     */
    public function loadView($viewName, $data = array())
    {
        //检测是否手机端
        if($this->is_mobile)
        {
            $this->load->view('home/mobile/'.$viewName, $data);
        }
        else
        {
            $this->load->view('home/pc/'.$viewName, $data);
        }
    }

    /**
     * 获取用户信息/重定向
     * @param  boolean $redirect 未登录时，admin:重定向到后台index, home重定向到前台index,否则不进行重定向
     * @param  string  $back_url 登陆成功后跳转的地址
     * @return (object) array            用户session信息,注意可能为空(未进行重定向时)
     */
    public function userSession($redirect = true, $back_url = null)
    {
        $this->load->library('session');
        $user = null;
        if($this->model_name == 'admin')
        {
            $user = $this->session->admin_web_user;
            if(empty($user) && $redirect)
            {
                header("Location:{$this->base_url}admin/account/login");exit;
            }
        }
        //前台的ajax请求接口才能放到ajax模块中
        else if($this->model_name == 'home' || $this->model_name == 'ajax')
        {
            $user = $this->session->web_user;
            if(empty($user) && $redirect)
            {
                $redirect_url = $this->base_url . 'home/account/login';
                empty($back_url) || $redirect_url = $redirect_url . '?back_url=' . $back_url; 
                header("Location:$redirect_url");exit;
            }
        }
        return $user;
    }

    public function response($data)
    {
        $this->load->helper('json_helper');
        echo api_jsonp_encode($data);
        exit;
    }
    /**
     * [redirect 跳转辅助函数]
     * @param  [type]  $url     [跳转地址]
     * @param  [type]  $message [提示信息]
     * @param  integer $time    [等待时间]
     * @return [type]           [description]
     */
    public function redirect($url=null,$message=null,$time=2)
    {
        $data =  array(
                'base_url'=>$this->base_url,
                'url'=>$this->model_name.'/'.$url,
                'heading'=>'提示信息',
                'message'=>isset($message)?$message:'sorry 出错了 请重试',
                'time'=>$time,
                );
        $this->load->view('errors/html/redirect',$data);
    }

    /**
     * 根据useragent判断是否是微信客户端
     * @return 是微信则返回openid,否则返回false
     */
    protected function isWeChat()
    {
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        if(strpos(strtolower($userAgent), 'micromessenger') !== false)
        {
            return true;
        }
        return false;
    }

    /**
     * 从session中获取openid
     * @return [type] [description]
     */
    protected function getOpenidFromSession()
    {
        $this->load->library('session');
        return $this->session->openid;
    }
}
